<?php
class Base 
{
	public $time;
	public $db;
	public $view;
	public $cache;
	
	function init() 
	{
		$this->_initDb();
		$this->_initView();
		$this->_initCookie();
		$this->_initCache();
	}
	
	private function _initDb()
	{
		require_once ROOT_DIR.'library'.DIRECTORY_SEPARATOR.'DB.php';
		$this->db = new DB(MC_DBHOST, MC_DBUSER, MC_DBPW, MC_DBNAME, MC_DBCHARSET, MC_DBCONNECT, MC_DBTABLEPRE);		
	}
	
	private function _initView()
	{
		require_once ROOT_DIR . 'library' . DIRECTORY_SEPARATOR . 'Template.php';
		$lang = get_vars('lang', 'zh', 'S');
		$skin = get_vars('skin', 'default', 'S');
		$this->view = new Template($skin, $lang);
		$this->view->assign('db_histories', $this->db->histories);
		$this->view->assign('db_querynum', $this->db->querynum);
		$this->view->assign('charset', MC_CHARSET);
	}
	
	private function _initCache()
	{
		require_once ROOT_DIR . 'library' . DIRECTORY_SEPARATOR . 'Cache.php';
		$this->cache = new Cache($this->db);
		$this->db->cache = $this->cache;
	}

	private function _initCookie()
	{
		require_once ROOT_DIR . 'library' . DIRECTORY_SEPARATOR . 'Cookie.php';
		$this->cookie = new Cookie(MC_COOKIE_EXPIRE, MC_COOKIE_PATH, MC_COOKIE_DOMAIN, MC_COOKIE_PREFIX);
	}
	/**
	 * Load Model, deposit $_ENV superglobal vars
	 *
	 * @param string $model module name
	 * @param object $base default
	 * @return void
	 */
	function load($model, $base = NULL) {
		$base = $base ? $base : $this;
		if (empty($_ENV[$model])) {
			require_once ROOT_DIR."modules".DIRECTORY_SEPARATOR.ucfirst($model)."Module.php";
			eval('$_ENV[$model] = new '.ucfirst($model).'Module($base);');
		}
		return $_ENV[$model];
	}
}
?>